//
//  THJMyDianJiaOrderVC.m
//  THJXHW
//
//  Created by 河南德艺 on 2018/9/18.
//  Copyright © 2018年 河南德艺. All rights reserved.
//
#import "THJMyDianJiaOrderVC.h"
#import "THJMyDianjiaOrderCell.h"

@interface THJMyDianJiaOrderVC ()<UITableViewDelegate,UITableViewDataSource>
{
    NSInteger btnIndex;
    UITableView * _tableview;
    NSString * type;
}
@property (weak, nonatomic) IBOutlet UIButton *btn1;
@property (weak, nonatomic) IBOutlet UIButton *btn2;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topConstant;
@property (weak, nonatomic) IBOutlet UIView *lineView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *lineLeftSpace;
@property(nonatomic,strong)NSMutableArray * dataArr;

@end
@implementation THJMyDianJiaOrderVC
-(NSMutableArray*)dataArr{
    if(!_dataArr){
        _dataArr = [[NSMutableArray alloc] init];
    }
    return _dataArr;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    btnIndex = 1;
    if(KIsiPhoneX){
        [self setXNavBarWithTitle:@"我的点价订单"];
    }else{
        [self setNavBarWithTitle:@"我的点价订单"];
    }
    self.topConstant.constant = NavbarHeight;
    [self getOrderListWithType:@"sell"];
    _tableview = [self setTableviewWithFrame:CGRectMake(0, NavbarHeight+44, ScreenWidth, ScreenHeight - NavbarHeight - 40) RegisterNibNameArr:@[@"THJMyDianjiaOrderCell"] ReuseIdentifierArr:@[@"cell"] RowHeight:190 BgColor:nil IsLoadMore:NO];
    _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    THJMyDianjiaOrderCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    NSDictionary * dic = self.dataArr[indexPath.row];
    cell.btn.tag = indexPath.row;
    [cell setContentWithDic:dic];
    [cell.btn addTarget:self action:@selector(CellBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
-(void)CellBtnAction:(UIButton*)btn{
    NSInteger index = btn.tag;
    NSLog(@"撤单编号:%ld",index);
    NSDictionary * dic = self.dataArr[index];
    NSLog(@"撤单信息:%@",dic);
    if([dic[@"status"] isEqual:@"-1"]){
        [self HUDShowMsg:@"已经撤单" AfterDelayDimiss:1];
        return;
    }else if([dic[@"status"] isEqual:@"1"]){
        [self HUDShowMsg:@"已审核通过无法取消订单" AfterDelayDimiss:1];
        return;
    }else if([dic[@"status"] isEqual:@"2"]){
        [self HUDShowMsg:@"订单已完成无法取消订单" AfterDelayDimiss:1];
        return;
    }else if([dic[@"status"] isEqual:@"-2"]){
        [self HUDShowMsg:@"订单已失效" AfterDelayDimiss:1];
        return;
    }
    [self SVHUDShow];
    NSString * url = [NSString stringWithFormat:@"%@%@",THJDJUrlDomain,@"dingjia.php?f=Member&r=Member&m=che"];
    NSDictionary * paraDic = @{
                               @"id":dic[@"id"]
                               };
    [THJNetWork postDataFromUrl:url parameters:paraDic Token:[THJTools getToken] callback:^(NSDictionary* receiveData, NSError *error) {
        [self SVHUDHiden];
        NSLog(@"撤单-%@",receiveData);
        if([receiveData[@"status"] isEqual:@"200"]){
//            self.dataArr = receiveData[@"data"];
            if(btnIndex == 1){
                [self getOrderListWithType:@"sell"];
            }else{
                [self getOrderListWithType:@"buy"];
            }
        }
    }];
}
-(void)getOrderListWithType:(NSString*)type{
    [self SVHUDShow];
    NSString * url = [NSString stringWithFormat:@"%@%@",@"https://dianjia.thjb.net/dingjia.php?f=Member&r=Member&m=order_list&act=",type];
    [THJNetWork getNetDataWithTokenFromUrl:url callback:^(NSDictionary* receiveData, NSError *error) {
        [self SVHUDHiden];
        NSLog(@"点价单列表-%@",receiveData);
        if([receiveData[@"status"] isEqual:@"200"]){
            self.dataArr = receiveData[@"data"];
            [_tableview reloadData];
        }else if([receiveData[@"status"] isEqual:@"0"]){
            [self HUDShowMsg:@"当前没有订单" AfterDelayDimiss:2];
            self.dataArr = (NSMutableArray*)@[];
             [_tableview reloadData];
        }
    }];
}
- (IBAction)btn1Action:(id)sender {
    NSLog(@"%ld",btnIndex);
    if(btnIndex == 1){
        NSLog(@"001");
        return;
    }else{
        type = @"sell";
        NSLog(@"001--");
        btnIndex = 1;
        self.lineLeftSpace.constant = 0;
        [self getOrderListWithType:@"sell"];
    }
}
- (IBAction)btn2Action:(id)sender {
    NSLog(@"&&&-%ld",btnIndex);
    if(btnIndex == 2){
        NSLog(@"002");
        return;
    }else{
        type = @"buy";
        NSLog(@"002--");
        btnIndex = 2;
        self.lineLeftSpace.constant = ScreenWidth/2;
        [self getOrderListWithType:@"buy"];
    }
}
@end
